//
//  RWCategoryViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/26.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWCategoryViewController.h"
#import "AttributeView.h"
#import "UIView+Extnesion.h"
#import "RWCategoryList2ViewController.h"

#import "RWCategoryModel.h"
#import "FTYNavigationController.h"
#import "RWLoginViewController.h"

@interface RWCategoryViewController ()<AttributeViewDelegate>

//产地
@property (nonatomic, weak) AttributeView *productionPlaceV;
//葡萄品种
@property (nonatomic, weak) AttributeView *grapeBreedV;
//葡萄酒类别
@property (nonatomic, weak) AttributeView *wineCategoryV;

@property (nonatomic, copy) NSMutableArray *categoryModels;
@property (nonatomic, copy) NSMutableArray *productionPlaceNames;
@property (nonatomic, copy) NSMutableArray *productionPlaceIDs;
@property (nonatomic, copy) NSMutableArray *grapeBreedNames;
@property (nonatomic, copy) NSMutableArray *grapeBreedIDs;
@property (nonatomic, copy) NSMutableArray *wineCategoryNames;
@property (nonatomic, copy) NSMutableArray *wineCategoryIDs;


@end

@implementation RWCategoryViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigation];
    [self initObjects];
    //[self loadDatas];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([FTYUser sharedUser].userID.length > 0) {
        
        [self loadDatas];
    } else {
        
        RWLoginViewController *loginVC = [[RWLoginViewController alloc] init];
        FTYNavigationController *loginNC = [[FTYNavigationController alloc] initWithRootViewController:loginVC];
        loginNC.navigationBar.translucent = NO;
        loginNC.navigationBar.barTintColor = [FTYColor getMainColorWithAlpha:1.];
        loginNC.navigationBar.tintColor = [UIColor whiteColor];
        [loginNC.navigationBar setTitleTextAttributes:@{NSFontAttributeName: FTY_FONTTSIZE(20),
                                                        NSForegroundColorAttributeName: [UIColor whiteColor]}];
        [self presentViewController:loginNC animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{

    _categoryModels = [NSMutableArray array];
    _productionPlaceNames = [NSMutableArray array];
    _productionPlaceIDs = [NSMutableArray array];
    _grapeBreedNames = [NSMutableArray array];
    _grapeBreedIDs = [NSMutableArray array];
    _wineCategoryNames = [NSMutableArray array];
    _wineCategoryIDs = [NSMutableArray array];
    
    _categoryModels = [[NSMutableArray alloc] init];
}

- (void)clearData
{
    
    if (_categoryModels) {
        [_categoryModels removeAllObjects];
    }
    
    if (_productionPlaceNames) {
        [_productionPlaceNames removeAllObjects];
    }
    
    if (_productionPlaceIDs) {
        [_productionPlaceIDs removeAllObjects];
    }
    
    if (_grapeBreedNames) {
        [_grapeBreedNames removeAllObjects];
    }
    
    if (_grapeBreedIDs) {
        [_grapeBreedIDs removeAllObjects];
    }
    
    if (_wineCategoryNames) {
        [_wineCategoryNames removeAllObjects];
    }
    
    if (_wineCategoryIDs) {
        [_wineCategoryIDs removeAllObjects];
    }
    
}

- (void)loadDatas{

    [FTYHTTPManager getInfoWithSubUrl:@"/shop/ybkapi.php?act=ybkapi&op=classlist" parameters:nil block:^(id result, NSError *error) {
        
        if (!error) {
        
            DLog(@"分类====%@", result);
            [self clearData];
            //_categoryModels = [FTYBaseModel mj_objectWithKeyValues:result].info;
            _categoryModels = [NSMutableArray arrayWithArray:[FTYBaseModel mj_objectWithKeyValues:result].info];
            //产地
            NSMutableArray *tempPs = [RWCategoryModel mj_objectWithKeyValues:_categoryModels[0]].value;
            for (NSDictionary *tempPDic in tempPs) {
                [_productionPlaceNames addObject:[RWCategoryDetailModel mj_objectWithKeyValues:tempPDic].sp_value_name];
                [_productionPlaceIDs addObject:[RWCategoryDetailModel mj_objectWithKeyValues:tempPDic].sp_value_id];
            }
            //葡萄品种
            NSMutableArray *tempGs = [RWCategoryModel mj_objectWithKeyValues:_categoryModels[1]].value;
            for (NSDictionary *tempGDic in tempGs) {
                [_grapeBreedNames addObject:[RWCategoryDetailModel mj_objectWithKeyValues:tempGDic].sp_value_name];
                [_grapeBreedIDs addObject:[RWCategoryDetailModel mj_objectWithKeyValues:tempGDic].sp_value_id];
            }
            //葡萄酒分类
            NSMutableArray *tempCs = [RWCategoryModel mj_objectWithKeyValues:_categoryModels[2]].value;
            for (NSDictionary *tempCDic in tempCs) {
                [_wineCategoryNames addObject:[RWCategoryDetailModel mj_objectWithKeyValues:tempCDic].sp_value_name];
                [_wineCategoryIDs addObject:[RWCategoryDetailModel mj_objectWithKeyValues:tempCDic].sp_value_id];
            }
            
            [self setCategoryUI];
        } else {
            
            NSLog(@"分类界面请求错误：%@", error);
        }
    }];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"分类";
}

- (void)setCategoryUI{
    
    UIScrollView *bottomSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 1)];
    [self.view addSubview:bottomSV];
    bottomSV.backgroundColor = [FTYColor whiteColor];
    
    //产地视图
    AttributeView *productionPlaceV = [AttributeView attributeViewWithTitle:@"国家" titleFont:FTY_FONTTSIZE(15) attributeTexts:_productionPlaceNames viewWidth:FTY_SCREEN_WIDTH];
    productionPlaceV.title = @"国家";
    [bottomSV addSubview:productionPlaceV];
    productionPlaceV.Attribute_delegate = self;
    _productionPlaceV = productionPlaceV;
    
    //葡萄品种
    AttributeView *grapeBreedV = [AttributeView attributeViewWithTitle:@"葡萄品种" titleFont:FTY_FONTTSIZE(15) attributeTexts:_grapeBreedNames viewWidth:FTY_SCREEN_WIDTH];
    grapeBreedV.title = @"葡萄品种";
    [bottomSV addSubview:grapeBreedV];
    grapeBreedV.Attribute_delegate = self;
    _grapeBreedV = grapeBreedV;
    
    //葡萄酒类别
    AttributeView *wineCategoryV = [AttributeView attributeViewWithTitle:@"葡萄酒类别" titleFont:FTY_FONTTSIZE(15) attributeTexts:_wineCategoryNames viewWidth:FTY_SCREEN_WIDTH];
    wineCategoryV.title = @"葡萄酒类别";
    [bottomSV addSubview:wineCategoryV];
    wineCategoryV.Attribute_delegate = self;
    _wineCategoryV = wineCategoryV;
    
    // 这里不用设置attriButeView的frame, 只需要设置y值就可以了,而且Y值是必须设置的,高度是已经在内部计算好的.可以更改宽度.
    productionPlaceV.y = 0;
    grapeBreedV.y = CGRectGetMaxY(productionPlaceV.frame) + 2.*FTY_HEIGHT_BASE;
    wineCategoryV.y = CGRectGetMaxY(grapeBreedV.frame) + 2.*FTY_HEIGHT_BASE;
    
    bottomSV.contentSize = (CGSize){0, FTY_SCREEN_HEIGHT + 4.*FTY_HEIGHT_BASE};
    
    // 通过动画设置scrollview的高度, 也可以一开始就设置好
    [UIView animateWithDuration:.5 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        bottomSV.height += FTY_SCREEN_HEIGHT - 64.*FTY_HEIGHT_BASE - 4.*FTY_HEIGHT_BASE;
    } completion:nil];
}

#pragma mark - Config Invoke Methods

#pragma mark - Notificaiton Invoke Method

#pragma mark - Private Invoke Methods

#pragma mark - <UITableViewDataSource>

#pragma mark - <UITableViewDelegate>

#pragma mark - <AttributeViewDelegate>

- (void)Attribute_View:(AttributeView *)view didClickBtn:(UIButton *)btn{
    
    NSInteger index = btn.tag;
    NSString *spValueID = @"";
    if ([view.title isEqualToString:@"国家"]) {
        spValueID = _productionPlaceIDs[index];
    } else if ([view.title isEqualToString:@"葡萄品种"]){
        spValueID = _grapeBreedIDs[index];
    } else if([view.title isEqualToString:@"葡萄酒类别"]){
        spValueID = _wineCategoryIDs[index];
    }

    RWCategoryList2ViewController *categoryListVC = [[RWCategoryList2ViewController alloc] init];
    categoryListVC.hidesBottomBarWhenPushed = YES;
    categoryListVC.className = view.title;
    categoryListVC.spValueID = spValueID;
    [self.navigationController pushViewController:categoryListVC animated:YES];
}

#pragma mark - Private Invoke Methods

@end
